﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.Content;
using Android.Hardware;
using Android.Provider;
using Android.Runtime;
using Android.Util;
using Android.Views;
using GBU.Services.Common;
using Android.Locations;

namespace GBU.Services.StepService
{
    public class AcceleraView:View,ISensorEventListener
    {
        public event EventHandler<AccelerationEventArgs> OnAcceleraChanged;

        private StepCounter stepCounter;

        public AcceleraView(Context context) : base(context)
        {
           stepCounter=new StepCounter();
            stepCounter.SetSensitivity(15);
           stepCounter.OnStep += new EventHandler(stepCounter_OnStep);
        }

        void stepCounter_OnStep(object sender, EventArgs e)
        {
            if (OnAcceleraChanged != null)
            {
                OnAcceleraChanged(this, new AccelerationEventArgs(){Steps = 1});
            }
        }


        public void OnAccuracyChanged(Sensor sensor, SensorStatus accuracy)
        {
            //throw new NotImplementedException();
        }

        public void OnSensorChanged(SensorEvent e)
        {
            if (e.Sensor.Type == SensorType.Accelerometer)
            {

                float[] vs=new float[e.Values.Count];
                var msg = string.Empty;
                for (int i = 0; i < e.Values.Count;i++ )
                {
                    vs[i] = e.Values[i];
                    msg += vs[i].ToString() + ",";
                }
               // Log.Debug("AcceleraView", msg);
               stepCounter.ComputeStep(vs);
            }
        }
    }
}
